package cn.sdormitory.knowledgebase.service.impl;

import cn.sdormitory.knowledgebase.dao.RuleRegulationDao;
import cn.sdormitory.knowledgebase.service.RuleRegulationService;
import cn.sdormitory.knowledgebase.vo.RuleRegulationVo;
import cn.sdormitory.knowledgebase.dto.RuleRegulationDto;
import cn.sdormitory.knowledgebase.pojo.RuleRegulation;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("ruleRegulationService")
public class RuleRegulationServiceImpl extends ServiceImpl<RuleRegulationDao, RuleRegulation> implements RuleRegulationService {


    @Autowired
    private RuleRegulationDao ruleRegulationDao;
    @Override
    public IPage<RuleRegulationVo> getRuleRegulationList(Integer pageNum, Integer pageSize, String title, Integer type) {
        // 创建分页对象
        Page<RuleRegulation> page = new Page<>(pageNum, pageSize);

        // 构建查询条件
        LambdaQueryWrapper<RuleRegulation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type != null && type != 0,
                        RuleRegulation::getType, type)
                .like(StringUtils.isNotBlank(title),
                        RuleRegulation::getTitle, title);
        wrapper.orderByDesc(RuleRegulation::getCreateTime);

        // 执行分页查询
        Page<RuleRegulation> ruleRegulation = ruleRegulationDao.selectPage(page, wrapper);

        // 转换为VO对象
        Page<RuleRegulationVo> voPage = new Page<>();
        BeanUtils.copyProperties(ruleRegulation, voPage, "records");

        // 转换记录
        List<RuleRegulationVo> voList = ruleRegulation.getRecords().stream()
                .map(entity -> {
                    RuleRegulationVo vo = new RuleRegulationVo();
                    BeanUtils.copyProperties(entity, vo);
                    return vo;
                })
                .collect(Collectors.toList());

        voPage.setRecords(voList);

        return voPage;
    }
    @Override
    public Integer addRuleRegulation(RuleRegulationDto regulationDto) {
        return ruleRegulationDao.addRuleRegulation(regulationDto);
    }

    @Override
    public Integer updateRuleRegulation(RuleRegulationDto regulationDto) {
        return ruleRegulationDao.updateRuleRegulation(regulationDto);
    }

    @Override
    public Integer deleteRuleRegulation(Integer id) {
        return ruleRegulationDao.deleteRuleRegulation(id);
    }

    @Override
    public RuleRegulation getRuleRegulationById(Integer id) {
        return ruleRegulationDao.getRuleRegulationById(id);
    }

    @Override
    public Integer deleteList(Integer[] rIds) {
        return ruleRegulationDao.deleteBatchIds(Arrays.asList(rIds));
    }

}


